package com.yyjj.my.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.UpdatePasswordCallback;
import com.yyjj.my.R;
import com.yyjj.my.cache.CacheData;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.ToastUtil;
@Route(path = "/my/ChangePassword")
public class ChangePasswordActivity extends BaseTitleActivity {

    private final int ERROR_CODE_INCORRECT_OLD_PASSWORD = 210;//旧密码有误

    EditText etOldPw;
    EditText etNewPw;
    EditText etComfirmPw;

    private AVUser mUser;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText(getResources().getString(R.string.password_upData))
                .setOnLeftTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onBackPressed();
                    }
                })
                .setStatusBarLightMode(false)
                .setRightText(getResources().getString(R.string.ok))
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        sure();
                    }
                });
    }


    @Override
    public int getContentLayout() {
        return R.layout.my_activity_change_password;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
//        mUser = CacheData.getLoginUser();
        mUser = AVUser.getCurrentUser();
        etOldPw = findViewById(R.id.et_old_pw);
        etNewPw = findViewById(R.id.et_new_pw);
        etComfirmPw = findViewById(R.id.et_comfirm_pw);
    }

    public void sure() {
        String oldPw = etOldPw.getText().toString();
        final String newPw = etNewPw.getText().toString();
        String comfirmPw = etComfirmPw.getText().toString();

        //旧密码校验
        if (DataUtils.isEmptyString(oldPw)) {
            ToastUtil.show(getResources().getString(R.string.put_old_pw));
            return;
        }
//        getResources().getString(R.string.)
        //新密码校验
        if (DataUtils.isEmptyString(newPw)) {
            ToastUtil.show(  getResources().getString(R.string.put_new_pw));
            return;
        }

        //新密码校验
        if (DataUtils.isEmptyString(comfirmPw)) {
            ToastUtil.show(getResources().getString(R.string.input_new_pw));
            return;
        }

        if (newPw.length() < 6) {
            ToastUtil.show(getResources().getString(R.string.input_new_pw_changdu));
            return;
        }

        if (!newPw.equals(comfirmPw)) {
            ToastUtil.show(getResources().getString(R.string.pw_inconformity));
            return;
        }

        mUser.updatePasswordInBackground(oldPw, newPw, new UpdatePasswordCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    String password = mUser.getString("password");
                    CacheData.setLoginPassword(mContext, newPw);
                    ToastUtil.show(getResources().getString(R.string.pw_upData_succeed));
                    finish();
                } else {
                    if (ERROR_CODE_INCORRECT_OLD_PASSWORD == e.getCode()) {
                        ToastUtil.show(getResources().getString(R.string.old_pw_error));
                    } else {
                        ToastUtil.show(getResources().getString(R.string.upData_password_fail));
                    }
                }
            }
        });
        finish();
    }
}
